﻿@model IEnumerable<Digital.Contact.Models.TemplateModel>

@{
    ViewBag.Title = "Index";
    ViewBag.IsTemplate = "class=open";
    Layout = "~/Views/Shared/_LayoutDigital.cshtml";
}

<h2>Index</h2>

<div class="row">

    <div class="col-md-12">

        <div class="widget">
            <div class="widget-head">
			    <div class="pull-left">
                    @using (Html.BeginForm("Index", "Template", FormMethod.Post, new { @class = "form-inline" }))
                    {
                        @Html.AntiForgeryToken()
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="用户名" name="name" value="@ViewBag.Search" />

                        </div>
                        <button type="submit" class="btn btn-default">搜 索</button>

                    }
                </div>
                <div class="pull-right"><a href="~/Template/Edit" class="btn btn-danger">
    创建
</a></div>
                <div class="widget-icons pull-right">
                  
                </div>
                <div class="clearfix"></div>
            </div>

            <div class="widget-content">

                <table class="table table-striped table-bordered table-hover">
                    <thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TableName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.IsCreate)
        </th>
        <th>控制</th>
    </tr>
	 </thead>
	 <tbody>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TableName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsCreate)
        </td>
        <td>
             <a href="~/Template/Edit?Id=@item.TemplateID" class="btn btn-xs btn-warning"><i class="icon-pencil"></i> </a>
             <button onclick="javascript:Delete(@item.TemplateID)" data-toggle="modal" class="btn btn-xs btn-danger"><i class="icon-remove"></i> </button>
		  
        </td>
    </tr>
}
</tbody>
</table>
 <input type="hidden" id="DataId" />
                @Html.AntiForgeryToken()
                <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                <h4 class="modal-title">提示消息</h4>
                            </div>
                            <div class="modal-body">

                                <p id="ModelContent">是否删除？</p>
                            </div>
                            <div class="modal-footer">
                                <button type="button" id="CancelButton" class="btn btn-default" data-dismiss="modal" aria-hidden="true">取消</button>
                                <button type="button" id="OkButton" onclick="javascript: DeletePost()" class="btn btn-primary">确 定</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="AlertModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                <h4 class="modal-title">提示消息</h4>
                            </div>
                            <div class="modal-body">

                                <p id="AlertContent">是否删除？</p>
                            </div>

                            <div class="modal-footer">
                                <button type="button" onclick="callBack()" id="CancelButton" class="btn btn-default" data-dismiss="modal" aria-hidden="true">确定</button>
                            </div>
                        </div>
                    </div>
                </div>
                <script>
                    var IsRefresh = false;
                    function callBack() {

                        if (IsRefresh) {
                            window.location.reload();
                        }
                    }


                    function Delete(Id) {
                        $("#ModelContent").text("是否删除？");
                        $("#myModal").modal();
                        $("#DataId").val(Id);
                    }
                    function DeletePost() {

                        $('#myModal').modal('hide');
                        $.ajaxAntiForgery({
                            type: "post",
                            data: { Id: $("#DataId").val() },
                            url: "/Template/Delete",
                            success: function (data) {
                                if (data) {
                                    IsRefresh = true;
                                    AlertMsg("ok", "删除成功");

                                }
                                else {
                                    IsRefresh = false;
                                    AlertMsg("error", "删除失败");
                                }
                            }
                        })
                    }



                    function AlertMsg(type, msg) {
                        if (type == "error") {
                            $("#AlertDiv").attr("class", "alert alert-success");
                        }
                        else {
                            $("#AlertDiv").attr("class", "alert alert-warning");
                        }
                        $("#AlertContent").text(msg);
                        $("#AlertModal").modal("show");
                    }

                </script>
<div class="widget-foot">
                     <ul class="pagination pull-right">
					 @{int PrevIndex = 1; int NextIndex = 1;}
                        @{
                            if (@ViewBag.PageIndex == 1)
                            {
                                PrevIndex = 1;
                            }
                            else
                            {
                                PrevIndex = @ViewBag.PageIndex - 1;
                            }
                            if (ViewBag.PageCount == ViewBag.PageIndex)
                            {
                                NextIndex = @ViewBag.PageIndex;
                            }
                            else
                            {
                                NextIndex = @ViewBag.PageIndex+1;
                            }
                        }
						 @if (@ViewBag.PageCount > 1)
                        {
                           <li><a href="~/Template/Index/@PrevIndex?name=@ViewBag.Search">上一页</a></li>
						}
                        @for (int i = 1; i <= @ViewBag.PageCount; i++)
                        {
                            if (ViewBag.PageIndex == i)
                            {
                                <li class="active">
                                    <a href="~/Template/Index/@i?name=@ViewBag.Search">@i</a>
                                </li>
                            }
                            else
                            {
                                <li>
                                    <a href="~/Template/Index/@i?name=@ViewBag.Search">@i</a>
                                </li>
                            }

                        }
						@if (@ViewBag.PageCount > 1 && ViewBag.PageIndex != @ViewBag.PageCount)
                        {
                           <li><a href="~/Template/Index/@NextIndex?name=@ViewBag.Search">下一页</a></li>
						}
                    </ul>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</div>
